package com.vjmobi.test.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.vjmobi.test.entity.User;
import com.vjmobi.test.service.UserService;
import com.vjmobi.test.vo.ApiRequest;
import com.vjmobi.test.vo.ApiResult;
import com.vjmobi.test.vo.page.PageReq;
import com.vjmobi.test.vo.page.PageResp;

@RequestMapping("/user")
@RestController
public class UserController {
	private static final Logger logger = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private  UserService userService;
	@PostMapping(value = "/list")
	public ApiResult<PageResp<User>> list(@RequestBody ApiRequest<PageReq<User>> param) {
		logger.info("分页查询用户信息！！参数：{}",param);
		return ApiResult.success(userService.findList(param.getData()));
	}
	@PostMapping(value="/add")
	public ApiResult<String> add(@RequestBody ApiRequest<User> param){
		logger.info("添加用户信息！！参数：{}",param);
		User user = param.getData();
		if(user == null){
			return ApiResult.error("用户信息不能为空");
		}
		if(StringUtils.isEmpty(user.getName())) {
			return ApiResult.error("用户名不能为空");
		}
		if(StringUtils.isEmpty(user.getAge())) {
			return ApiResult.error("用户年龄不能为空");
		}
		if(StringUtils.isEmpty(user.getBirthday())) {
			return ApiResult.error("用户生日不能为空");
		}
		try {
			int count = userService.add(user);
			if(1 == count) {
				return ApiResult.success("保存成功");
			}
		} catch (Exception e) {
			logger.error("用户信息保存失败",e);
		}
		return ApiResult.error("用户信息保存失败");
	}
	

}
